﻿cmake_minimum_required(VERSION 3.25)
project(mqttQT)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
#     set(CMAKE_PREFIX_PATH "C:/Qt/5.15.2/msvc2019_64")
#     add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/source-charset:utf-8>")
#     add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/execution-charset:utf-8>")

# else()
#      set(CMAKE_PREFIX_PATH "C:/Qt/5.15.2/mingw81_64")
# endif()
# if(CMAKE_SIZEOF_VOID_P EQUAL 8)
#     set(platform x64)
# else()
#     set(platform x86)
# endif()

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 REQUIRED COMPONENTS Widgets Qmqtt)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS
        Core
        Gui
        Widgets
        Qmqtt
        REQUIRED
    PrintSupport)

set(PROJECT_SOURCES
        main.cpp
        mainwidget.cpp
        mainwidget.h
        mainwidget.ui
        backend/Mmqtt.cpp
        backend/Mmqtt.h
        mcustomplot.h
        mcustomplot.cpp
        )
if(ANDROID)
    add_library(mqttQT SHARED ${PROJECT_SOURCES})
else()
    add_executable(mqttQT ${PROJECT_SOURCES})
endif()

# 指定QCustomPlot库的路径
set(QCUSTOMPLOT_INCLUDE_DIR ${CMAKE_CURRENT_LIST_DIR}/lib/qcustomplot/include)
set(QCUSTOMPLOT_LIB_DIR ${CMAKE_CURRENT_LIST_DIR}/lib/qcustomplot/${CMAKE_CXX_COMPILER_ID}/${platform})
target_include_directories(mqttQT PRIVATE ${QCUSTOMPLOT_INCLUDE_DIR})
find_library(QCUSTOMPLOT_STATIC qcustomplot
             PATHS ${QCUSTOMPLOT_LIB_DIR}
             NO_DEFAULT_PATH)
if(NOT QCUSTOMPLOT_STATIC)
    message(FATAL_ERROR "Could not find QCustomPlot static library.")
endif()
message(${QCUSTOMPLOT_STATIC})
target_link_libraries(mqttQT
        Qt5::Core
        Qt5::Gui
        Qt5::Widgets
        Qt5::Qmqtt
        Qt5::PrintSupport
        ${QCUSTOMPLOT_STATIC}
        )
include_directories(${QCUSTOMPLOT_INCLUDE_DIR})
if (WIN32 AND NOT DEFINED CMAKE_TOOLCHAIN_FILE)
    set(DEBUG_SUFFIX)
    if (MSVC AND CMAKE_BUILD_TYPE MATCHES "Debug")
        set(DEBUG_SUFFIX "d")
    endif ()
    set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
    if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
        set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
            set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
        endif ()
    endif ()
    if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
    endif ()
    foreach (QT_LIB Core Gui Widgets)
        add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E copy
                "${QT_INSTALL_PATH}/bin/Qt5${QT_LIB}${DEBUG_SUFFIX}.dll"
                "$<TARGET_FILE_DIR:${PROJECT_NAME}>")
    endforeach (QT_LIB)
endif ()
